﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Database.Service;
using Portal.Web.Utility;
using Portal.Cms.Model;
using Portal.Core;

namespace Portal.Cms.Bussiness
{
    public class DocTypeProcessBll
    {
        public static DocTypeCls CreateDocTypeModel(string SiteId, string DocTypeId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TableDocType " +
                                               "     where DocTypeId=@DocTypeId order by SortIndex",
                    new DbParam[] 
                    { 
                        new DbParam("DocTypeId", DocTypeId),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            DocTypeCls ODocType = DocTypeParser.ParseFromDataRow(dsResult.Tables[0].Rows[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return ODocType;
        }



        public static DocTypeCls[] ReadingDocType(string SiteId, string GroupDataId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TableDocType where frkGroupDataId=@frkGroupDataId order by SortIndex",
                    new DbParam[] { new DbParam("frkGroupDataId", GroupDataId) });

            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new DocTypeCls[0];
            }

            DocTypeCls[] DocType = DocTypeParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return DocType;
        }


        public static void AddDocType(string SiteId, string GroupDataId, string DocTypeName, string SortIndex)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string DocTypeId = System.Guid.NewGuid().ToString();
            string AssetCode = WebSessionUtility.GetCurrentLoginUser().AssetCode;
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            DBService.Insert(null, "TableDocType",
                    new DbParam[] 
                    { 
                        new DbParam("DocTypeId", DocTypeId),
                        new DbParam("frkGroupDataId", GroupDataId),
                        new DbParam("DocTypeName", DocTypeName),
                        new DbParam("SortIndex", SortIndex),
                        new DbParam("AssetCode", AssetCode),
                        new DbParam("DataLanguage", DataLanguage),
                    });
            SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddSysLogProcess(
                SiteId,
                WebSessionUtility.GetCurrentLoginUser().AssetCode,
                "Dictionary", "Thêm mới loại văn bản " + DocTypeName, 
                WebSessionUtility.GetCurrentLoginUser().UserId,
                WebSessionUtility.GetCurrentLoginUser().LoginName, 
                WebSessionUtility.GetCurrentLoginUser().FullName);
        }


        public static void SaveDocType(string SiteId, string DocTypeId, string DocTypeName, string SortIndex)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DBService.Update(null, "TableDocType", "DocTypeId", DocTypeId,
                    new DbParam[] 
                    { 
                        new DbParam("DocTypeName", DocTypeName),
                        new DbParam("SortIndex", SortIndex),
                    });
            SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddSysLogProcess(SiteId,
                WebSessionUtility.GetCurrentLoginUser().AssetCode,
                "Dictionary", "Sửa chữa loại văn bản " + DocTypeName,
                WebSessionUtility.GetCurrentLoginUser().UserId,
                WebSessionUtility.GetCurrentLoginUser().LoginName,
                WebSessionUtility.GetCurrentLoginUser().FullName);
        }


        public static void DeleteDocType(string SiteId, string DocTypeId)
        {
            DocTypeCls ODocType = DocTypeProcessBll.CreateDocTypeModel(SiteId, DocTypeId);
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string Query =
                " Delete from TableDocType where DocTypeId=@DocTypeId; ";
            DBService.ExecuteNonQuery(null, Query,
                    new DbParam[] 
                    { 
                        new DbParam("DocTypeId", DocTypeId),
                    });
            SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddSysLogProcess(SiteId,
                WebSessionUtility.GetCurrentLoginUser().AssetCode,
                "Dictionary", "Xóa loại văn bản " + ODocType.DocTypeName,
                WebSessionUtility.GetCurrentLoginUser().UserId,
                WebSessionUtility.GetCurrentLoginUser().LoginName,
                WebSessionUtility.GetCurrentLoginUser().FullName);
        }
    }
}
